'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 农场相关的接口
 * @author sundbcn@gmail.com (sun debing)
 */

var constantUtil = require('../util/constantUtil');
var buildType = require('../consts/buildType');
var buildConf = require('../config/buildConf');

/**
 * 获粮仓最大可存储粮食
 * @param  {[type]} barn_lv [description]
 * @return {[type]}          [description]
 */
exports.getBarnStoreFood = function (barn_lv) {
    // var barn_lv_conf = buildConf.getBuildLvConf(buildType.BARN, barn_lv);
    // if (!barn_lv_conf) {
    //     return 0;
    // }
    // return barn_lv_conf.food_store_num;
    return 2000000000;
};

/**
 * 获取能收取的食物
 * @param  {[type]} gain_time_interval [description]
 * @return {[type]}                    [description]
 */
exports.getCanGainFood = function (farm_lv, second, max_gain_food) {
    var gain_food = 0;
    var leave_food = 0;

    // 获取农场可产出的数量
    var farm_food_store_num = _getFarmStoreFood(farm_lv);

    gain_food = parseInt(second * _getCanGainFoodEverySecond(farm_lv));

    // 判断是否超过农场的最大上限
    if (gain_food > farm_food_store_num) {
        gain_food = farm_food_store_num;
    }

    // 判断是否走过粮仓的最大上限
    if (gain_food > max_gain_food) {
        leave_food = gain_food - max_gain_food;
        gain_food = max_gain_food;
    }

    return {
        gain_food: gain_food,
        leave_food: leave_food
    };
};

/**
 * 获取每秒能收的粮食
 * @param  {[type]} farm_lv [description]
 * @param  {[type]} second  [description]
 * @return {[type]}         [description]
 */
exports.getCanGainFoodEverySecond = function (farm_lv) {
    return _getCanGainFoodEverySecond(farm_lv);
};

/**
 * 获取相应的食物需要多少秒回复
 * @return {[type]} [description]
 */
exports.getGainFoodNeedSecond = function (farm_lv, food) {
    return food / _getCanGainFoodEverySecond(farm_lv);
};

var _getCanGainFoodEverySecond = function (farm_lv) {
    // 农场每个时间段生成出来的粮食
    var farm_food_product_num = _getFarmProductFood(farm_lv);
    return farm_food_product_num / 3600;
};

/**
 * 农场每个时间段生成出来的粮食
 * @param  {[type]} mine_lv [description]
 * @return {[type]}         [description]
 */
var _getFarmProductFood = function (farm_lv) {
    var farm_lv_conf = buildConf.getBuildLvConf(buildType.FARM, farm_lv);
    return farm_lv_conf.product_food_num;
};

/**
 * 农场最大可生产的粮食
 * @param  {[type]} mine_lv [description]
 * @return {[type]}         [description]
 */
var _getFarmStoreFood = function (farm_lv) {
    var farm_lv_conf = buildConf.getBuildLvConf(buildType.FARM, farm_lv);
    return farm_lv_conf.food_store_num;
};
